## This code include scripts for Appendix Tables
# Table A9
# Table A8
# Table A11
# Table A10
# Table A16
# Table A17
# Table A12
# Table A13
# Table A14
# Table A15



rm(list = ls())

library(readr)
library(ggplot2)
library(stargazer)
library(lme4)
library(dplyr)
library(haven)
library(texreg)
library(tidyr)

load("Final_JPR/data/paperdata.RData")
data <- paperdata %>%
  filter(tek_count>0)

source("Final_JPR/Code/coefplot.R")
source("Final_JPR/Code/marginal_effects.R")
source("Final_JPR/Code/setup.R")


data <- data %>% dplyr::mutate(peaceyears_terr_sq = peaceyears_terr^2,
                               peaceyears_terr_cub = peaceyears_terr^3,
                               peaceyears_gov_sq = peaceyears_gov^2,
                               peaceyears_gov_cub = peaceyears_gov^3)

## dependent variable
dv <- 'onset_do_terr_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears_terr','peaceyears_terr_sq','peaceyears_terr_cub')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_terr<- list()
for (i in 1:length(ivs)){
       model_onset_terr[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data, family = binomial(link = "logit"))
}



dv <- 'onset_do_gov_flag' 


ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears_gov','peaceyears_gov_sq','peaceyears_gov_cub')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_gov<- list()
for (i in 1:length(ivs)){
       model_onset_gov[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                        data = data, family = binomial(link = "logit"))
}


## dependent variable
dv <- 'incidence_terr_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log',  'peaceyears_terr','peaceyears_terr_sq','peaceyears_terr_cub')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_terr<- list()
for (i in 1:length(ivs)){
       model_incidence_terr[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                   data = data, family = binomial(link = "logit"))
}



dv <- 'incidence_gov_flag' 


ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears_gov','peaceyears_gov_sq','peaceyears_gov_cub')

ivs_1 <- c('best_tlineq_total', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_gov<- list()
for (i in 1:length(ivs)){
       model_incidence_gov[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                  data = data, family = binomial(link = "logit"))
}


#### Table A8-A9

modSD = cluster_se(ModelResults = c(model_onset_terr,model_incidence_terr), data = data, 
                   clusterid = "gwgroupid") 

# Table A9
texreg(c(model_onset_terr,model_incidence_terr), file = "Final_JPR/tables/appendix_tables/Table_A9.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Heterogeneity analysis: Transnational inequality on territorial conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       override.se =  modSD,
       label = "tab:tab92",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Incidence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "peaceyears_terr" = "Peace Year",
                              "peaceyears_terr_sq" =  "Peace Year $^2$",
                              "peaceyears_terr_cub" = "Peace Year $^2$",
                              "worst_tlineq_total" = "Transnational inequality",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_tlineq_total" = "Transnational inequality",
                              "teknearst_status_excl" ="TEK status excluded",
                              "(Intercept)" = "Intercept"),
       digits = 3)

## to table  #### Table A8
modSD = cluster_se(ModelResults = c(model_onset_gov,model_incidence_gov), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_gov,model_incidence_gov), file = "Final_JPR/tables/appendix_tables/Table_A8.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Heterogeneity analysis: Transnational inequality on governmental conflict (1992-2020)",
       caption.above = TRUE,
       override.se =  modSD,
       label = "tab:tab102",
       scalebox = 0.7,
       use.packages = FALSE,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Incidence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                                                     "tekbest_status_excl" =  "TEK status excluded",
                                                     "lineq_total" = "Horizontal inequality",
                                                     "status_excl" =  "Status excluded",  
                                                     "lsize " =  "Relative group size",
                                                     "family_warhistdummy" =  "Previous rebellions",
                                                     "family_downgraded2" = "Status downgraded",
                                                     "ctygdppc_log" = "Ln(Country GDP per capita)",
                                                     "ctypop_log" = "Ln(Country population)",
                                                     "peaceyears_gov" = "Peace Year",
                                                     "peaceyears_gov_sq" =  "Peace Year $^2$",
                                                     "peaceyears_gov_cub" = "Peace Year $^2$",
                                                     "worst_tlineq_total" = "Transnational inequality",
                                                     "tekworst_status_excl" =  "TEK status excluded",
                                                     "median_tlineq_total" = "Transnational inequality",
                                                     "tekmedian_status_excl"  =  "TEK status excluded",
                                                     "nearst_tlineq_total" = "Transnational inequality",
                                                     "teknearst_status_excl" ="TEK status excluded",
                                                     "(Intercept)" = "Intercept"),
                              digits = 3)

### Table A10-A11

## Relative terms

## dependent variable
dv <- 'onset_do_terr_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','peaceyears_terr','peaceyears_terr_sq','peaceyears_terr_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative_terr <- list()
for (i in 1:length(ivs)){
  model_onset_relative_terr[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                        data = data, family = binomial(link = "logit"))
}


## dependent variable
dv <- 'onset_do_gov_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears_gov','peaceyears_gov_sq','peaceyears_gov_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_onset_relative_gov <- list()
for (i in 1:length(ivs)){
  model_onset_relative_gov[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                       data = data, family = binomial(link = "logit"))
}


#### incidence
dv <- 'incidence_terr_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log','peaceyears_terr','peaceyears_terr_sq','peaceyears_terr_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_indidence_relative_terr <- list()
for (i in 1:length(ivs)){
  model_indidence_relative_terr[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                            data = data, family = binomial(link = "logit"))
}


## dependent variable
dv <- 'incidence_gov_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears_gov','peaceyears_gov_sq','peaceyears_gov_cub')

ivs_1 <- c('best_low_tlineq', 'best_high_tlineq', 'tekbest_status_excl')
ivs_2 <- c('worst_low_tlineq', 'worst_high_tlineq', 'tekworst_status_excl')
ivs_3 <- c('median_low_tlineq', 'median_high_tlineq','tekmedian_status_excl')
ivs_4 <- c('nearst_low_tlineq', 'nearst_high_tlineq','teknearst_status_excl')


f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best','worst','median','nearst')


## using log of transnational inequality
model_incidence_relative_gov <- list()
for (i in 1:length(ivs)){
  model_incidence_relative_gov[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                           data = data, family = binomial(link = "logit"))
}



## To Table: terr  Table A11

modSD = cluster_se(ModelResults = c(model_onset_relative_terr,  model_indidence_relative_terr), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_relative_terr,  model_indidence_relative_terr), file = "Final_JPR/tables/appendix_tables/Table_A11.tex",
       stars = c(0.01, 0.05, 0.1),
       override.se =  modSD,
       caption = "Heterogeneity analysis: The Logistic Regression Results of Upward and Downward Comparisons for Ethnic territorial Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       label = "tab:terr1",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Incidence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "peaceyears_terr" = "Peace Year",
                              "peaceyears_terr_sq" =  "Peace Year $^2$",
                              "peaceyears_terr_cub" = "Peace Year $^2$",
                              "(Intercept)" = "Intercept"),
       digits = 3)

## government
## TABLE A10
modSD = cluster_se(ModelResults = c(model_onset_relative_gov,  model_incidence_relative_gov), data = data, 
                   clusterid = "gwgroupid") 

texreg(c(model_onset_relative_gov,  model_incidence_relative_gov), file = "Final_JPR/tables/appendix_tables/Table_A10.tex",
       stars = c(0.01, 0.05, 0.1),
       override.se =  modSD,
       caption = "Heterogeneity analysis: The Logistic Regression Results of Upward and Downward Comparisons for Ethnic governmental Conflict (1992-2020)",
       caption.above = TRUE,
       use.packages = FALSE,
       label = "tab:gov1",
       scalebox = 0.7,
       custom.header = list("DV: Conflict Onset" = 1:4,
                            "DV: Conflict Incidence" = 5:8),
       custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                              "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
       custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                              "best_high_tlineq" = "Relative wealth",
                              "tekbest_status_excl" =  "TEK status excluded",
                              "lineq_total" = "Horizontal inequality",
                              "status_excl" =  "Status excluded",  
                              "lsize " =  "Relative group size",
                              "family_warhistdummy" =  "Previous rebellions",
                              "family_downgraded2" = "Status downgraded",
                              "ctygdppc_log" = "Ln(Country GDP per capita)",
                              "ctypop_log" = "Ln(Country population)",
                              "worst_low_tlineq" ="Relative poverty",
                              "worst_high_tlineq" = "Relative wealth",
                              "tekworst_status_excl" =  "TEK status excluded",
                              "median_low_tlineq" = "Relative poverty",
                              "median_high_tlineq" =  "Relative wealth",
                              "median_tlineq_total" = "Transnational inequality",
                              "tekmedian_status_excl"  =  "TEK status excluded",
                              "nearst_low_tlineq" ="Relative poverty",
                              "nearst_high_tlineq" =  "Relative wealth",
                              "teknearst_status_excl" ="TEK status excluded",
                              "peaceyears_gov" = "Peace Year",
                              "peaceyears_gov_sq" =  "Peace Year $^2$",
                              "peaceyears_gov_cub" = "Peace Year $^2$",
                              "(Intercept)" = "Intercept"),
       digits = 3)





#########################################################################################
### Interaction with oil


data <- data %>% dplyr::mutate(oil_dum = ifelse(oil_giant_fields_count>0, 1, 0))
dv <- 'onset_do_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')

ivs_1 <- c('best_tlineq_total*oil_dum', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total*oil_dum', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total*oil_dum",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total*oil_dum','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_onset_oil <- list()
for (i in 1:length(ivs)){
        model_onset_oil[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                  data = data,family = binomial(link = "logit"))
}


dv <- 'incidence_flag' 
## controls
ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
          'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')

ivs_1 <- c('best_tlineq_total*oil_dum', 'tekbest_status_excl')
ivs_2 <- c('worst_tlineq_total*oil_dum', 'tekworst_status_excl')
ivs_3 <- c("median_tlineq_total*oil_dum",'tekmedian_status_excl')
ivs_4 <- c('nearst_tlineq_total*oil_dum','teknearst_status_excl')

f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

#
ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')


## using log of transnational inequality
model_incidence_oil <- list()
for (i in 1:length(ivs)){
        model_incidence_oil[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                   data = data,family = binomial(link = "logit"))
}
screenreg(model_incidence_oil)


 #Table A16
 modSD = cluster_se(ModelResults = c(model_onset_oil,model_incidence_oil), data = data, 
                     clusterid = "gwgroupid") 
  
 texreg(c(model_onset_oil,model_incidence_oil), file = "Final_JPR/tables/appendix_tables/Table_A16.tex",
         stars = c(0.01, 0.05, 0.1),
         caption = "Heterogeneity analysis: Logistic regression results of transnational inequality conditional on access to oil fields",
         caption.above = TRUE,
         label = "tab:tab72",
         scalebox = 0.65,
         use.packages = FALSE,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                                "oil_dum" = "Access to oil fields",
                                "best_tlineq_total:oil_dum" = "Transnational inequality $\\times$ Access to oil fields",
                                "worst_tlineq_total:oil_dum"= "Transnational inequality $\\times$ Access to oil fields",
                                "median_tlineq_total:oil_dum" = "Transnational inequality $\\times$ Access to oil fields",
                                "nearst_tlineq_total:oil_dum" = "Transnational inequality $\\times$ Access to oil fields",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "worst_tlineq_total" = "Transnational inequality",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_tlineq_total" = "Transnational inequality",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_tlineq_total" = "Transnational inequality",
                                "teknearst_status_excl" ="TEK status excluded",
                                "(Intercept)" = "Intercept"),
         digits = 3)

 ######################## Relative terms
  ## dependent variable
  dv <- 'onset_do_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*oil_dum', 'best_high_tlineq*oil_dum', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*oil_dum', 'worst_high_tlineq*oil_dum', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*oil_dum', 'median_high_tlineq*oil_dum','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*oil_dum', 'nearst_high_tlineq*oil_dum','teknearst_status_excl')
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_onset_relative <- list()
  for (i in 1:length(ivs)){
    model_onset_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data, family = binomial(link = "logit"))
  }
  
  
  ## dependent variable
  dv <- 'incidence_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*oil_dum', 'best_high_tlineq*oil_dum', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*oil_dum', 'worst_high_tlineq*oil_dum', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*oil_dum', 'median_high_tlineq*oil_dum','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*oil_dum', 'nearst_high_tlineq*oil_dum','teknearst_status_excl')
  
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_incidence_relative <- list()
  for (i in 1:length(ivs)){
    model_incidence_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data, family = binomial(link = "logit"))
  }
  
 
  
 #Table A17
  
  modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                     clusterid = "gwgroupid") 
  
  texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A17.tex",
         stars = c(0.01, 0.05, 0.1),
         override.se =  modSD,
         caption = "Heterogeneity analysis: Logistic regression results of upward and downward comparisons for ethnic conflict conditional on access to oil fields(1992-2020)",
         caption.above = TRUE,
         use.packages = FALSE,
         label = "tab:oil2",
         scalebox = 0.65,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                                "best_high_tlineq" = "Relative wealth",
                                "status_excl" = "Access to oil fields",
                                "best_low_tlineq:oil_dum" = "Relative poverty $\\times$ Access to oil fields",
                                "worst_low_tlineq:oil_dum"= "Relative poverty $\\times$ Access to oil fields",
                                "median_low_tlineq:oil_dum" = "Relative poverty $\\times$ Access to oil fields",
                                "nearst_low_tlineq:oil_dum" = "Relative poverty $\\times$ Access to oil fields",
                                "oil_dum:best_high_tlineq" = "Relative wealth $\\times$ Access to oil fields",
                                "oil_dum:worst_high_tlineq"= "Relative wealth $\\times$ Access to oil fields",
                                "oil_dum:median_high_tlineq" = "Relative wealth $\\times$ Access to oil fields",
                                "oil_dum:nearst_high_tlineq" = "Relative wealth $\\times$ Access to oil fields",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "worst_low_tlineq" ="Relative poverty",
                                "worst_high_tlineq" = "Relative wealth",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_low_tlineq" = "Relative poverty",
                                "median_high_tlineq" =  "Relative wealth",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_low_tlineq" ="Relative poverty",
                                "nearst_high_tlineq" =  "Relative wealth",
                                "teknearst_status_excl" ="TEK status excluded",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "(Intercept)" = "Intercept"),
         digits = 3)
  
  
  
  
  
  

  
   ### Political Status

  dv <- 'onset_do_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_tlineq_total*status_excl', 'tekbest_status_excl')
  ivs_2 <- c('worst_tlineq_total*status_excl', 'tekworst_status_excl')
  ivs_3 <- c("median_tlineq_total*status_excl",'tekmedian_status_excl')
  ivs_4 <- c('nearst_tlineq_total*status_excl','teknearst_status_excl')
 
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')
  
  
  ## using log of transnational inequality
  model_onset_status <- list()
  for (i in 1:length(ivs)){
          model_onset_status[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data,family = binomial(link = "logit"))
  }
  screenreg(model_onset_status)
  
  dv <- 'incidence_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_tlineq_total*status_excl', 'tekbest_status_excl')
  ivs_2 <- c('worst_tlineq_total*status_excl', 'tekworst_status_excl')
  ivs_3 <- c("median_tlineq_total*status_excl",'tekmedian_status_excl')
  ivs_4 <- c('nearst_tlineq_total*status_excl','teknearst_status_excl')
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')
  
  
  ## using log of transnational inequality
  model_incidence_status <- list()
  for (i in 1:length(ivs)){
          model_incidence_status[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                        data = data,family = binomial(link = "logit"))
  }
  screenreg(model_incidence_status)
  
  # Table A12
  
  modSD = cluster_se(ModelResults = c(model_onset_status,model_incidence_status), data = data, 
                     clusterid = "gwgroupid") 
  
  texreg(c(model_onset_status,model_incidence_status), file = "Final_JPR/tables/appendix_tables/Table_A12.tex",
         stars = c(0.01, 0.05, 0.1),
         caption = "Heterogeneity analysis: Logistic regression results of transnational inequality conditional on political exclusion",
         caption.above = TRUE,
         override.se =  modSD,
         label = "tab:tab82",
         scalebox = 0.7,
         use.packages = FALSE,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                                "best_tlineq_total:status_excl" = "Transnational inequality $\\times$ Status excluded",
                                "worst_tlineq_total:status_excl"= "Transnational inequality $\\times$ Status excluded",
                                "median_tlineq_total:status_excl" = "Transnational inequality $\\times$ Status excluded",
                                "nearst_tlineq_total:status_excl" = "Transnational inequality $\\times$ Status excluded",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "worst_tlineq_total" = "Transnational inequality",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_tlineq_total" = "Transnational inequality",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_tlineq_total" = "Transnational inequality",
                                "teknearst_status_excl" ="TEK status excluded",
                                "(Intercept)" = "Intercept"),
         digits = 3)
  
  # Table A13
  ### relative terms
  
  ## dependent variable
  dv <- 'onset_do_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*status_excl', 'best_high_tlineq*status_excl', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*status_excl', 'worst_high_tlineq*status_excl', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*status_excl', 'median_high_tlineq*status_excl','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*status_excl', 'nearst_high_tlineq*status_excl','teknearst_status_excl')
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_onset_relative <- list()
  for (i in 1:length(ivs)){
    model_onset_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data, family = binomial(link = "logit"))
  }
  
  
  ## dependent variable
  dv <- 'incidence_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*status_excl', 'best_high_tlineq*status_excl', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*status_excl', 'worst_high_tlineq*status_excl', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*status_excl', 'median_high_tlineq*status_excl','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*status_excl', 'nearst_high_tlineq*status_excl','teknearst_status_excl')
  
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_incidence_relative <- list()
  for (i in 1:length(ivs)){
    model_incidence_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data, family = binomial(link = "logit"))
  }
  
  screenreg(c(model_onset_relative, model_incidence_relative ))
  
  modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                     clusterid = "gwgroupid") 
  # Table A13
  
  texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A13.tex",
         stars = c(0.01, 0.05, 0.1),
         override.se =  modSD,
         caption = "Heterogeneity analysis: Logistic regression results of upward and downward comparisons for ethnic conflict conditional on political exclusion(1992-2020)",
         caption.above = TRUE,
         use.packages = FALSE,
         label = "tab:status",
         scalebox = 0.65,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                                "best_high_tlineq" = "Relative wealth",
                                "best_low_tlineq:status_excl" = "Relative poverty $\\times$ Status excluded",
                                "worst_low_tlineq:status_excl"= "Relative poverty $\\times$ Status excluded",
                                "median_low_tlineq:status_excl" = "Relative poverty $\\times$ Status excluded",
                                "nearst_low_tlineq:status_excl" = "Relative poverty $\\times$ Status excluded",
                                "status_excl:best_high_tlineq" = "Relative wealth $\\times$ Status excluded",
                                "status_excl:worst_high_tlineq"= "Relative wealth $\\times$ Status excluded",
                                "status_excl:median_high_tlineq" = "Relative wealth $\\times$ Status excluded",
                                "status_excl:nearst_high_tlineq" = "Relative wealth $\\times$ Status excluded",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "worst_low_tlineq" ="Relative poverty",
                                "worst_high_tlineq" = "Relative wealth",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_low_tlineq" = "Relative poverty",
                                "median_high_tlineq" =  "Relative wealth",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_low_tlineq" ="Relative poverty",
                                "nearst_high_tlineq" =  "Relative wealth",
                                "teknearst_status_excl" ="TEK status excluded",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "(Intercept)" = "Intercept"),
         digits = 3)
  
  
  
  
  
  
  
  
  
  #############################################################################################################
 ## Interaction with domestic horizontal inequality
  dv <- 'onset_do_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_tlineq_total*lineq_total', 'tekbest_status_excl')
  ivs_2 <- c('worst_tlineq_total*lineq_total', 'tekworst_status_excl')
  ivs_3 <- c("median_tlineq_total*lineq_total",'tekmedian_status_excl')
  ivs_4 <- c('nearst_tlineq_total*lineq_total','teknearst_status_excl')
 
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))

  #
  ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')
  
  
  ## using log of transnational inequality
  model_onset_hi <- list()
  for (i in 1:length(ivs)){
          model_onset_hi[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data,family = binomial(link = "logit"))
  }
  screenreg(model_onset_hi)
  
  
  dv <- 'incidence_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log', 'peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_tlineq_total*lineq_total', 'tekbest_status_excl')
  ivs_2 <- c('worst_tlineq_total*lineq_total', 'tekworst_status_excl')
  ivs_3 <- c("median_tlineq_total*lineq_total",'tekmedian_status_excl')
  ivs_4 <- c('nearst_tlineq_total*lineq_total','teknearst_status_excl')
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
 
  #
  ivs <- c('best_tlineq_total','worst_tlineq_total','median_tlineq_total','nearst_tlineq_total')
  
  
  ## using log of transnational inequality
  model_incidence_hi <- list()
  for (i in 1:length(ivs)){
          model_incidence_hi[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data,family = binomial(link = "logit"))
  }
  screenreg(model_incidence_hi)
  
  ## Table A14
  modSD = cluster_se(ModelResults = c(model_onset_hi,model_incidence_hi), data = data, 
                     clusterid = "gwgroupid") 
  texreg(c(model_onset_hi,model_incidence_hi), file = "Final_JPR/tables/appendix_tables/Table_A14.tex",
         stars = c(0.01, 0.05, 0.1),
         override.se =  modSD,
         caption = "Heterogeneity analysis: Logistic regression Results of transnational inequality conditional on domestic horizontal inequality",
         caption.above = TRUE,
         use.packages = FALSE,
         label = "tab:tab112",
         scalebox = 0.7,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_tlineq_total" = "Transnational inequality",
                                "best_tlineq_total:lineq_total" = "Transnational inequality $\\times$ Horizontal inequality",
                                "worst_tlineq_total:lineq_total"= "Transnational inequality $\\times$ Horizontal inequality",
                                "median_tlineq_total:lineq_total" = "Transnational inequality $\\times$ Horizontal inequality",
                                "nearst_tlineq_total:lineq_total" = "Transnational inequality $\\times$ Horizontal inequality",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "worst_tlineq_total" = "Transnational inequality",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_tlineq_total" = "Transnational inequality",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_tlineq_total" = "Transnational inequality",
                                "teknearst_status_excl" ="TEK status excluded",
                                "(Intercept)" = "Intercept"),
         digits = 3)
  
  ## relative terms
  dv <- 'onset_do_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*lineq_total', 'best_high_tlineq*lineq_total', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*lineq_total', 'worst_high_tlineq*lineq_total', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*lineq_total', 'median_high_tlineq*lineq_total','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*lineq_total', 'nearst_high_tlineq*lineq_total','teknearst_status_excl')
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_onset_relative <- list()
  for (i in 1:length(ivs)){
    model_onset_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                     data = data, family = binomial(link = "logit"))
  }
  
  
  ## dependent variable
  dv <- 'incidence_flag' 
  ## controls
  ctrs <- c('lineq_total', 'status_excl','lsize','family_warhistdummy','family_downgraded2', 
            'ctygdppc_log', 'ctypop_log','peaceyears','peaceyears_sq','peaceyears_cub')
  
  ivs_1 <- c('best_low_tlineq*lineq_total', 'best_high_tlineq*lineq_total', 'tekbest_status_excl')
  ivs_2 <- c('worst_low_tlineq*lineq_total', 'worst_high_tlineq*lineq_total', 'tekworst_status_excl')
  ivs_3 <- c('median_low_tlineq*lineq_total', 'median_high_tlineq*lineq_total','tekmedian_status_excl')
  ivs_4 <- c('nearst_low_tlineq*lineq_total', 'nearst_high_tlineq*lineq_total','teknearst_status_excl')
  
  
  
  f_do_1 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_1, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_2 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_2, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_3 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_3, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  f_do_4 <- formula(paste0(dv, ' ~ ', paste(paste(ivs_4, collapse=' + '), paste(ctrs, collapse=' + '), sep =' + ')))
  
  #
  ivs <- c('best','worst','median','nearst')
  
  
  ## using log of transnational inequality
  model_incidence_relative <- list()
  for (i in 1:length(ivs)){
    model_incidence_relative[[i]] <- glm(eval(parse(text = paste0('f_do_', i))), 
                                         data = data, family = binomial(link = "logit"))
  }
  
  screenreg(c(model_onset_relative, model_incidence_relative ))
  
  # Table A15
  modSD = cluster_se(ModelResults = c(model_onset_relative, model_incidence_relative), data = data, 
                     clusterid = "gwgroupid") 
  
  texreg(c(model_onset_relative, model_incidence_relative ), file = "Final_JPR/tables/appendix_tables/Table_A15.tex",
         stars = c(0.01, 0.05, 0.1),
         override.se =  modSD,
         caption = "Heterogeneity analysis: Logistic regression results of upward and downward comparisons for ethnic conflict conditional on domestic horizontal inequality",
         caption.above = TRUE,
         use.packages = FALSE,
         label = "tab:reltivehi",
         scalebox = 0.65,
         custom.header = list("DV: Conflict Onset" = 1:4,
                              "DV: Conflict Incidence" = 5:8),
         custom.model.names = c("M1:Best", "M2:Worst", "M3:Median", "M4:Nearest",
                                "M5:Best", "M6:Worst", "M7:Median", "M8:Nearest"),
         custom.coef.map = list("best_low_tlineq" = "Relative poverty",
                                "best_high_tlineq" = "Relative wealth",
                                "best_low_tlineq:lineq_total" = "Relative poverty $\\times$ Horizontal inequality",
                                "worst_low_tlineq:lineq_total"= "Relative poverty $\\times$ Horizontal inequality",
                                "median_low_tlineq:lineq_total" = "Relative poverty $\\times$ Horizontal inequality",
                                "nearst_low_tlineq:lineq_total" = "Relative poverty $\\times$ Horizontal inequality",
                                "lineq_total:best_high_tlineq" = "Relative wealth $\\times$ Horizontal inequality",
                                "lineq_total:worst_high_tlineq"= "Relative wealth $\\times$ Horizontal inequality",
                                "lineq_total:median_high_tlineq" = "Relative wealth $\\times$ Horizontal inequality",
                                "lineq_total:nearst_high_tlineq" = "Relative wealth $\\times$ Horizontal inequality",
                                "tekbest_status_excl" =  "TEK status excluded",
                                "lineq_total" = "Horizontal inequality",
                                "status_excl" =  "Status excluded",  
                                "lsize " =  "Relative group size",
                                "family_warhistdummy" =  "Previous rebellions",
                                "family_downgraded2" = "Status downgraded",
                                "ctygdppc_log" = "Ln(Country GDP per capita)",
                                "ctypop_log" = "Ln(Country population)",
                                "worst_low_tlineq" ="Relative poverty",
                                "worst_high_tlineq" = "Relative wealth",
                                "tekworst_status_excl" =  "TEK status excluded",
                                "median_low_tlineq" = "Relative poverty",
                                "median_high_tlineq" =  "Relative wealth",
                                "tekmedian_status_excl"  =  "TEK status excluded",
                                "nearst_low_tlineq" ="Relative poverty",
                                "nearst_high_tlineq" =  "Relative wealth",
                                "teknearst_status_excl" ="TEK status excluded",
                                "peaceyears" = "Peace Year",
                                "peaceyears_sq" =  "Peace Year $^2$",
                                "peaceyears_cub" = "Peace Year $^2$",
                                "(Intercept)" = "Intercept"),
         digits = 3)
  
  
  
  
  
 